<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1>登录页面</h1>
    <input type="text" v-model="user.username" placeholder="用户名"><br>
    <input type="password" v-model="user.password" placeholder="密码"><br>
    记住用户名和密码<input v-model="user.rem" type="checkbox"><br>
    <input type="button" value="登录" @click="login()">
</div>
<!--引入Vue框架-->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!--引入Axios框架-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script>
    let v = new Vue({
        el:"body>div",
        data:{
            user:{
                username:"",
                password:"",
                rem:false
            }
        },
        methods:{
            login(){
                axios.post("/login",v.user).then(function (response) {
                    if (response.data==1){
                        alert("登录成功!");
                        location.href = "/";
                    }else if(response.data==2){
                        alert("用户名不存在!");
                    }else{
                        alert("密码错误!");
                    }
                })
            }
        },
        created:function () {
            //取出Cookie里面的用户名和密码显示到页面中
            //'password=123456; username=tom'
            let arr = document.cookie.split(";");
            //遍历数组  ["username=tom"," password=123456"]
            for (let cookie of arr) {
                let cookieArr = cookie.split("=");
                let key = cookieArr[0].trim();
                let value = cookieArr[1];
                if (key=="username"){
                    this.user.username = value;
                }
                if (key=="password"){
                    this.user.password = value;
                }
            }
        }
    })
</script>
</body>
</html>